/*
 * 成绩录入
 */
package com.res0w.SMMS.admin;

import com.res0w.SMMS.ds.Rank;
import com.res0w.SMMS.ds.Result;
import com.res0w.SMMS.ds.SettingScore;
import com.res0w.SMMS.factory.DataBaseOperate;
import com.res0w.SMMS.factory.EveryScoreCal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author res0w
 * @since 2009-9-22
 * @version 0.2
 * 
 */
public class ResultInput {

    private DataBaseOperate db;
    private ArrayList sqlList = new ArrayList();
    public String atheleteId;
    public String result;
    public String itemsName;

    public ResultInput() {
        db = new DataBaseOperate();
        this.sqlList.clear();
    }

    public void onlyExe() {
        db.dbUpdateBatch(sqlList);
    }

    public void ResultWrite() {
        Result rs = new Result();
        rs.atheleteId = atheleteId;
        rs.itemsName = itemsName;
        rs.result = result;
        for (int i = 0; i < rs.exeSqls().length; i++) {
            sqlList.add(rs.exeSqls()[i]);
        }
    }

    public void updateRank() {
        try {
            //在cacheLIst中更新得分
            calResult();
            //loadAcademy
            ArrayList<Rank> academyScoreList = new ArrayList<Rank>();
            ResultSet rs=null;
            try {
                String Items = "select distinct academy from signup_cachelist";
                DataBaseOperate d = new DataBaseOperate();
                for(int i=0;i<d.dbQuerySolo(Items,false).size();i++){
                    String score="select score from signup_cachelist where " +
                            "academy='"+d.dbQuerySolo(Items,false).get(i).toString()+"'";
                    DataBaseOperate di = new DataBaseOperate();
                    rs=di.dbQueryMuti(score);
                    int sumScoreInt=0;
                    while(rs.next()){
                        sumScoreInt=sumScoreInt+rs.getInt(1);
                    }
                    Rank rankTemp=new Rank();
                    rankTemp.acedemy=d.dbQuerySolo(Items,false).get(i).toString();
                    rankTemp.sumScore=String.valueOf(sumScoreInt);
                    academyScoreList.add(i, rankTemp);
                }
               
            } catch (SQLException ex) {
                Logger.getLogger(ResultInput.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (SQLException ex) {
            Logger.getLogger(ResultInput.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private ArrayList loadItems() {
        ArrayList itemsList = new ArrayList();
        try {
            String Items = "select distinct joinItems from signup_cachelist";
            DataBaseOperate d = new DataBaseOperate();
            itemsList = d.dbQuerySolo(Items, true);
        } catch (SQLException ex) {
            Logger.getLogger(ResultInput.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemsList;
    }
    //暂时只使用“越小越优”逻辑测试

    private ArrayList<Result> loadScore(String items, String order) throws SQLException {
        ArrayList returnList = new ArrayList();
        Result scoreDS = new Result();
        ResultSet rs = null;
        String sql = "select * from signup_cachelist where joinItems='" + items + "' order by result '" + order + "'";
        DataBaseOperate la = new DataBaseOperate();
        rs = la.dbQueryMuti(sql);
        while (rs.next()) {
            scoreDS.itemsName = rs.getString("joinItems");
            scoreDS.atheleteId = rs.getString("athleteId");
            returnList.add(scoreDS);
            scoreDS.clear();
        }
        return returnList;
    }

    private void calResult() throws SQLException {
        DataBaseOperate cr = new DataBaseOperate();
        ArrayList sqlArray = new ArrayList();
        ArrayList scoreList = new ArrayList();
        SettingScore ss = new SettingScore();
        ss.readSqls();
        scoreList = EveryScoreCal.everyScore(ss.maxScore, ss.minScore, ss.peopleCount);
        for (int i = 0; i < loadItems().size(); i++) {
            ArrayList<Result> scoreItemsList = new ArrayList<Result>();
            scoreItemsList = loadScore(loadItems().get(i).toString(), "ASC");
            for (int k = 0; k < scoreList.size(); k++) {
                scoreItemsList.get(k).score = scoreList.get(k).toString();
            }
            for (int m = 0; m < scoreItemsList.size(); m++) {
                String score = scoreItemsList.get(m).score;
                String Id = scoreItemsList.get(m).atheleteId;
                String name = scoreItemsList.get(m).itemsName;
                String updateSql = "update signup_cachelist set score='" + score + "' " +
                        "where joinItems='" + name + "' and athleteId='" + Id + "'";
                sqlArray.add(updateSql);
            }
        }
        cr.dbUpdateBatch(sqlArray);
    }
}
